package com.woniuxy.core.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtUtil {

    //自定义的秘钥
    //HS256：即字节长度>=32
    private static String sect ="ksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";

    private static String adminSecret ="adminfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";

    private static String userSecret ="usersfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";

    //生成token
    //键值对包含用户信息
    //以分钟为单位的时间 代表jwt的有效时间
    public static String createToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }

    public static String createToken(Map<String, Object> body,String secret,Integer mil) {
        Key key = Keys.hmacShaKeyFor(secret.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }

    //解析token
    public static Claims parseToken(String token, String secret) {
        Key key = Keys.hmacShaKeyFor(secret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    public static Claims parseToken(String token) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }



    /**
     * 生成AdminToken
     *
     */
    public static String createAdminToken(Map<String, Object> body) {
        return createToken(body,adminSecret,180);
    }
    public static String createAdminToken(Object... body){
        if (body == null || body.length<=0){
            throw new RuntimeException("没有主体数据");
        }
        HashMap<String, Object> maps = new HashMap<>();
        for (int i = 0; i < body.length; i+=2) {
            Object value = null;
            if(i+1 < body.length){
                value = body[i+1];
            }
            maps.put(body[i].toString(),value);
        }
        return createToken(maps,adminSecret,180);
    }

    /**
     * 生成UserToken
     *
     */
    public static String creatUserToken(Map<String, Object> body) {
        return createToken(body,userSecret,180);
    }
    public static String creatUserToken(Object... body){
        if (body == null || body.length<=0){
            throw new RuntimeException("没有主体数据");
        }
        HashMap<String, Object> maps = new HashMap<>();
        for (int i = 0; i < body.length; i+=2) {
            Object value = null;
            if(i+1 < body.length){
                value = body[i+1];
            }
            maps.put(body[i].toString(),value);
        }
        return createToken(maps,userSecret,180);
    }

    /**
     * 解析AdminToken
     * @param token
     * @return
     */
    public static Claims parseAdminToken(String token){
        return parseToken(token,adminSecret);
    }


    /**
     * 解析UserToken
     * @param token
     * @return
     */
    public static Claims parseUserToken(String token){
        return parseToken(token,userSecret);
    }

    public static void main(String[] args){
        HashMap<String, Object> body = new HashMap<String, Object>();
        body.put("id",1);

        String token = creatUserToken(body);

        System.out.println(token);
    }

}
